package com.example.work_02;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import java.util.Timer;
import java.util.TimerTask;

public class ProgressActivity extends AppCompatActivity {
    final static int Max=100;
    ProgressDialog progressDialog;
    Handler handler;
    int status=0;
    Timer timer=new Timer();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.progress);
        Button btn=(Button) findViewById(R.id.btn);
        handler=new Handler(){
            @Override
            public void handleMessage(Message msg){
                super.handleMessage(msg);
                switch (msg.what){
                    case 0:
                        progressDialog.setProgress(status);
                        break;
                    case 1:
                        progressDialog.dismiss();
                        break;
                }
            }
        };
    }

        public void showProgress(View v){
            status=0;
            progressDialog=new ProgressDialog(ProgressActivity.this);
            progressDialog.setMax(Max);
            progressDialog.setTitle("进度对话框");
            progressDialog.setMessage("已完成进度");
            progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progressDialog.setIndeterminate(false);
            progressDialog.show();
            timer.schedule(task,1000,1000);
        }
        TimerTask task=new TimerTask() {
            @Override
            public void run() {
                status++;
                if(status<Max){
                    handler.sendEmptyMessage(0);
                }else {
                    handler.sendEmptyMessage(1);
                }
            }
        };

}
